<?php
# XML parser and reader for info files.

# info.xml
# info.txt
# info.php

class info
{
	private $is_valid = false;

	# The key in XML that got the error.
	# When certain data is empty, error exists.
	private $error = '';
	
	public $name     = '';    # Project name
	public $client   = '';    # Name of the client/customer
	public $begun    = '';    # YYYY-MM-DD stamp of the date the project began
	public $comments = '';    # Project comments
	public $rename   = false; # Whether to use cusomized name, $name?
	public $disabled = false; # Is this project disabled one?
	
	/**
	* Immediately load an xml file and read it.
	*/
	public function __construct($xml_file_name='/tmp/*info.xml')
	{
		if($this->is_valid = is_file($xml_file_name))
		{
			$info = new SimpleXMLElement(file_get_contents($xml_file_name));
			
			# This list is same as our declaration above
			$data_keys = array(
				'name',
				'client',
				'begun',
				'comments',
				'rename',
				'disabled',
			);
			
			foreach($data_keys as $k => $key)
			{
				if(property_exists($info, $key))
				{
					# Use it as a string only, not an object
					$this->$key = trim("{$info->$key}");
					
					# If any key is missing, quit with error
					if($this->$key=='')
					{
						# Data cannot be emtpy
						$this->error = $key;
						$this->is_valid = false;
						break;
					}
				}
				else
				{
					# All desired keys should exist
					$this->error = $key;
					$this->is_valid = false;
					break;
				}
			}
		}
	}
	
	/**
	* Tell whether there was an error reading the xml information file.
	*/
	public function is_valid()
	{
		# This flag tells that the XML has explained all required indices.
		return $this->is_valid===true;
	}

	/**
	* Show the alternative name?
	*/
	public function do_rename()
	{
		return strtolower($this->rename)=='yes';
	}

	/**
	* Whether to enlist this project directory or not
	*/
	public function is_disabled()
	{
		return strtolower($this->disabled)=='yes';
	}
}
?>